<template>
	<view class="more" :style="{paddingTop: paddingHeight + 'px'}">
		<nav-bar></nav-bar>
		<view class="title">
			很高兴见到您! 告诉我们更多信息
		</view>
		<view class="sub_title">
			这有助于我们确保计算您的肺容量以获得准确的结果
		</view>
		<view class="c-cantainer">
			<view class="weight" @click="handleModal('weight')">
				<u-input shape="circle" inputAlign="right" readonly v-model="userInfo.weight" type="digit">
					<template slot="prefix">
						<text class="prefix">体重</text>
					</template>
				</u-input>
			</view>
			<view class="stature" @click="handleModal('height')">
				<u-input shape="circle" inputAlign="right" readonly v-model="userInfo.height">
					<template slot="prefix">
						<text class="prefix">身高</text>
					</template>
				</u-input>
			</view>
		</view>
		<view class="gender">
			<image @click="userInfo.sex = 1"
				:src="userInfo.sex == 1 ? `${imgUrl}img/ico/at/20230829/1693275534415.png` : `${imgUrl}img/ico/at/20230829/1693275516951.png`"
				class="man" />
			<image @click="userInfo.sex = 2"
				:src="userInfo.sex == 2 ? `${imgUrl}img/ico/at/20230829/1693275571450.png` : `${imgUrl}img/ico/at/20230829/1693275547874.png`"
				class="woman" />
		</view>
		<view class="next" @click="next">
			<uni-icons type="arrow-right" color="#fffF" size="50"></uni-icons>
		</view>
		<custom-modal :showModal="showModal" :defaultWeight="defaultWeight" :defaultHeight="defaultHeight"
			@close="showModal = false" :type="type" @sendWeight="setWeight" @sendHeight="setHeight"></custom-modal>
	</view>
</template>

<script>
	import customModal from '@/components/custom-modal/index.vue'
	export default {
		data() {
			return {
				paddingHeight: uni.getStorageSync('navbarHeight'),
				imgUrl: this.$imgUrl,
				showModal: false,
				type: 'weight',
				maxWLength: 5,
				userInfo: {
					weight: null,
					height: null,
					sex: 1,
				},
				defaultWeight: 65,
				defaultHeight: 175
			}
		},
		components: {
			customModal
		},
		onLoad(OPTIONS) {
			console.log('OPTIONS', OPTIONS)
			this.userInfo = Object.assign({}, this.userInfo, JSON.parse(OPTIONS.userInfo))
			let str = this.userInfo.birthday.replace(/年|月/g, '-').replace(/日/g, '')
			let arr = str.split('-')
			let newarr = arr.map((item, index) => {
				if (index != 0) {
					if (item.length <= 1) {
						return '0' + item
					} else {
						return item
					}
				} else {
					return item
				}
			})
			this.userInfo.birthday = newarr.join('-')
		},
		watch: {
			'userInfo.sex': {
				handler(newVal, oldVal) {
					if (newVal == 2) {
						this.defaultWeight = 50
						this.defaultHeight = 160
					} else if (newVal == 1) {
						this.defaultWeight = 65
						this.defaultHeight = 175
					}
				}
			}
		},
		methods: {
			handleModal(type) {
				this.type = type
				this.showModal = true
			},
			setWeight(val) {
				this.showModal = false
				this.userInfo.weight = val
			},
			setHeight(val) {
				this.showModal = false
				this.userInfo.height = String(val)
			},
			next() {
				if (!this.userInfo.weight || !this.userInfo.height) {
					uni.showToast({
						title: '请填写信息',
						icon: 'none'
					})
					return
				}
				uni.navigateTo({
					url: `/pagesB/login/info/sleep?userInfo=${JSON.stringify(this.userInfo)}`
				})

			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f7f7f7;
	}
</style>
<style lang="scss" scoped>
	.more {
		display: flex;
		align-items: flex-start;
		justify-content: center;
		flex-direction: column;
		padding: 0 80rpx 0;

		.title {
			font-size: 48rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #333333;
			margin-top: 45rpx;
			letter-spacing: 1px;
			text-align: left;
		}

		.sub_title {
			font-size: 32rpx;
			color: #333333;
			text-align: left;
			letter-spacing: 0.67px;
			margin-top: 16rpx;
		}

		.c-cantainer {
			margin-top: 176rpx;
			width: 100%;

			/deep/.u-input {
				padding: 0 50rpx 0 !important;
				height: 112rpx;
				background-color: #FAFAFA;
				margin-top: 48rpx;

				.u-input__content {
					line-height: 112rpx !important;
				}
			}
		}

		.gender {
			width: 100%;
			margin-top: 48rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			.woman,
			.man {
				width: 274rpx;
				height: 112rpx;
			}

			.woman {
				margin-left: 44rpx;
			}
		}
	}
</style>